using System.Text.Json;

namespace planning_lite;

public class Settings
{
    public string taskServerLocation { get; set; } = "http://localhost:9961/tasks";
    public string userName { get; set; } = "Guest";
    public bool sendPlanningMessage = false;

    public Settings()
    {
        taskServerLocation = "http://localhost:9961/tasks";
        userName = "Guest";
    }

    public Settings ReadCurrentSettings()
    {
        Settings result = new Settings();
        var filePath = Path.Combine(FileSystem.AppDataDirectory, "settings.json");
        try
        {
            if (File.Exists(filePath))
            {
                string jsonContent = File.ReadAllText(filePath);
                result = JsonSerializer.Deserialize<Settings>(jsonContent) ?? new Settings();
            }
            else
            {
                result = new Settings();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error loading settings: {ex.Message}");
        }
        return result;
    }

    public void SaveCurrentSettings(Settings settings)
    {
        var filePath = Path.Combine(FileSystem.AppDataDirectory, "settings.json");
        try
        {
            string jsonContent = JsonSerializer.Serialize(settings);
            File.WriteAllText(filePath, jsonContent);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error saving settings: {ex.Message}");
        }
    }
}